<?php
/*
+------------------------------------------------------------------------------+
| Copyright (c) 2009, Draftrack Team                                           |
| All rights reserved.                                                         |
|                                                                              |
| Redistribution and use in source and binary forms, with or without           |
| modification, are permitted provided that the following conditions are met:  |
|     * Redistributions of source code must retain the above copyright         |
|       notice, this list of conditions and the following disclaimer.          |
|     * Redistributions in binary form must reproduce the above copyright      |
|       notice, this list of conditions and the following disclaimer in the    |
|       documentation and/or other materials provided with the distribution.   |
|     * Neither the name of the <organization> nor the                         |
|       names of its contributors may be used to endorse or promote products   |
|       derived from this software without specific prior written permission.  |
|                                                                              |
| THIS SOFTWARE IS PROVIDED BY draftrack team ''AS IS'' AND ANY                |
| EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED    |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE       |
| DISCLAIMED. IN NO EVENT SHALL draftrack team BE LIABLE FOR ANY               |
| DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES   |
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND  |
| ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS|
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                 |
+------------------------------------------------------------------------------+

 * @category   Zend_extension
 * @package    Zend_Controller
 * @subpackage Zend_Controller_Action
*/

class Auth_IndexController extends Zend_Controller_Action
{
    function init()
    {
		$this->getHelper('viewRenderer')->setNoRender();
		Zend_Registry::get('app_log')->log('entering module:Auth controller:Index ', Zend_Log::DEBUG);
    }

    function indexAction()
    { 	

		$consumer = new Zend_Oauth_Consumer(Zend_Registry::get('app_config')->oauth);  		
		$consumer->setOptions(array( 'requestScheme' => Zend_Oauth::REQUEST_SCHEME_QUERYSTRING));
		   	
    	try{
			$token = $consumer->getRequestToken();

			if(!$token->isValid()) {
				require_once('Zend/Oauth/Exception.php');
				throw new Zend_Oauth_Exception('request token is not valid. token:'.$token);
			}
			
			Zend_Registry::get('app_log')->log('got request token:'.$token, Zend_Log::DEBUG);
			Zend_Registry::get('app_session')->requestToken = serialize($token);
    	} catch (Zend_Oauth_Exception $e) {
    		echo 'pb while getting request token';
    		print_r($e->getMessage());
			Zend_Registry::get('app_log')->log('Issue while getting request token: '.$e->getMessage(),4);
    		exit;	
    	}
		
		Zend_Registry::get('app_log')->log('redirecting to backend', Zend_Log::DEBUG);		
		$consumer->redirect();     
    	exit;
    	
    }

    function loginAction()
    {
    	echo 'You need to login';
    }    
    
    function callbackAction()
    {
    	$this->getHelper('viewRenderer')->setNoRender();


		$consumer = new Zend_Oauth_Consumer(Zend_Registry::get('app_config')->oauth);  
		$consumer->setOptions(array( 'requestScheme' => Zend_Oauth::REQUEST_SCHEME_QUERYSTRING));
		
		$oauth_token = $this->_getParam('oauth_token');
		$oauth_verifier = $this->_getParam('oauth_verifier');
		
		if (!empty($oauth_token) && !empty($oauth_verifier) && isset(Zend_Registry::get('app_session')->requestToken)) {

    	 try{
    	   $token = $consumer->getAccessToken($_GET, unserialize(Zend_Registry::get('app_session')->requestToken));
		   
		   Zend_Registry::get('app_log')->log('got access token:'.$token, Zend_Log::DEBUG);	
           Zend_Registry::get('app_session')->accessToken = serialize($token);
           unset(Zend_Registry::get('app_session')->requestToken);
           
           //Will try to get user details
           $engine = Myzen_Oauth_Query_Singleton::getInstance();
    	   $res = $engine->query('user','index','getdetails');

Zend_Registry::get('app_log')->log('got user details:'.$res, Zend_Log::DEBUG);	
		   
    	   $data = unserialize($res);
    	   Zend_Registry::get('app_session')->userDetails = $data['user'];
           
           $this->_redirect('');
           return;
    	 } catch (Zend_Oauth_Exception $e) {
    		echo 'pb while getting access token';
    		print_r($e->getMessage());
    		exit;	
    	}

		} else {
		  echo 'one or more parameters are missing to perform the action';
		  exit;	
		}	
		       
    
    }
    
    function logoutAction() {
      if(!isset(Zend_Registry::get('app_session')->accessToken)) {
        echo 'You are already logged out';
        exit;	
      }

      Zend_Session::destroy();
      echo 'You are now logged out';
      $this->_redirect($this->view->baseURL());
          	
    }
    

} //End class


?>